package jetty9_proxy;

import java.util.Properties;

import org.eclipse.jetty.server.Handler;
import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.server.handler.ContextHandlerCollection;
import org.eclipse.jetty.util.resource.Resource;
import org.eclipse.jetty.webapp.Configuration;
import org.eclipse.jetty.webapp.WebAppContext;

public class Main {

	public static void main(String[] args) throws Exception {
		// 获取properties
		Properties properties = new Properties();
		properties.load(Main.class.getClassLoader().getResourceAsStream("config.properties"));

		// 创建Server对象，并绑定端口
		Server server = new Server(Integer.parseInt(properties.getProperty("web.port", "8080")));
		
		// web
		WebAppContext context0 = new WebAppContext();
		context0.setContextPath("/");
		context0.setParentLoaderPriority(true);
		context0.setBaseResource(Resource.newResource(properties.getProperty("webapp.dir", "")));

        Configuration.ClassList classlist = Configuration.ClassList.setServerDefault(server);
        classlist.clear();
        classlist.add("org.eclipse.jetty.webapp.WebXmlConfiguration");
        classlist.add("org.eclipse.jetty.annotations.AnnotationConfiguration");

        // 静态资源
//        ResourceHandler context0 = new ResourceHandler();
//        context0.setBaseResource(Resource.newResource(properties.getProperty("webapp.dir", "")));
        
		ContextHandlerCollection contexts = new ContextHandlerCollection();
		contexts.setHandlers(new Handler[] { context0 });
		server.setHandler(contexts);

		// 启动
		server.start();

		// 打印dump时的信息
//		System.out.println(server.dump());
		System.out.println("jetty容器启动完成!");
		// join当前线程
		server.join();
	}

}
